package StreamOperation;

/*
 * Author: JacksonCoder
 * FileName: StreamOfTerminal
 * Version: 1.0.0
 * Date: 2025/1/24
 */

import java.util.*;
import java.util.stream.Collectors;

public class StreamOfTerminal {

    public static void main(String[] args) {
        ForEachExample();
        CollectExample();
        ReduceExample();
        CountExample();
        AnyMatchExample();
        AllMatchExample();
        NoneMatchExample();
        FindFirstExample();
        FindAnyExample();
    }

    private static void ForEachExample() {
        List<String> names = Arrays.asList("james", "bob", "alen", "chris", "jack", "jerry", "lucy", "alex", "lily", "tom", "tony");

        names.stream()
                .forEach(System.out::println);
    }

    private static void CollectExample() {
        List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David", "Eve");

        // 将流转换为 Set
        Set<String> uniqueNames = names.stream()
                .collect(Collectors.toSet());
        System.out.println("Name Set: " + uniqueNames);
    }

    private static void ReduceExample() {
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

        // 计算所有数字的和

        int sum = numbers.stream()
                .reduce(0, Integer::sum);
        System.out.println("Sum: " + sum);
    }

    private static void CountExample() {
        List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David", "Eve");

        // 统计名字个数
        long count = names.stream()
                .count();

        System.out.println(count);
    }

    private static void AnyMatchExample() {
        List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David", "Eve");

        // 检查是否有名字以 "C" 开头
        boolean anyMatch = names.stream()
                .anyMatch(name -> name.startsWith("C"));
        System.out.println("Any name starts with 'C': " + anyMatch);
    }

    private static void AllMatchExample() {
        List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David", "Eve");

        // 检查是否所有名字的长度都大于 3
        boolean allMatch = names.stream()
                .allMatch(name -> name.length() > 3);
        System.out.println("All names have length > 3: " + allMatch);
    }

    private static void NoneMatchExample() {
        List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David", "Eve");

        // 检查是否没有名字以 "Z" 开头
        boolean noneMatch = names.stream()
                .noneMatch(name -> name.startsWith("Z"));
        System.out.println("No name starts with 'Z': " + noneMatch);
    }

    private static void FindFirstExample() {
        List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David", "Eve");

        // 查找第一个长度大于 3 的名字
        Optional<String> firstMatch = names.stream()
                .filter(name -> name.length() > 3)
                .findFirst();
        firstMatch.ifPresent(System.out::println);
    }

    private static void FindAnyExample() {
        List<String> names = Arrays.asList("Charlie", "Alice", "Bob", "David", "Eve");
        // 查找任意一个长度大于 3 的名字
        Optional<String> anyMatch = names.stream()
                .filter(name -> name.length() > 3)
                .findAny();
        anyMatch.ifPresent(System.out::println);
    }

}
